package smedi.ismedi.web.bff.controller.constructionproject;

import com.smedi.ismedi.constructionproject.api.dto.request.ProjectBasicRequest;
import com.smedi.ismedi.constructionproject.api.dto.request.ProjectBasicUpdateRequest;
import com.smedi.ismedi.constructionproject.api.dto.request.ProjectListQueryRequest;
import com.smedi.ismedi.constructionproject.api.dto.request.ProjectPageQueryRequest;
import com.smedi.ismedi.constructionproject.api.dto.response.ProjectAssignmentResponse;
import com.smedi.ismedi.constructionproject.api.dto.response.ProjectBasicResponse;
import com.smedi.ismedi.constructionproject.api.dto.response.ProjectTreeResponse;
import com.smedi.ismedi.constructionproject.api.dto.response.resp.ProjectDetailResp;
import com.smedi.ismedi.constructionproject.api.dto.response.resp.ProjectResp;
import com.smedi.ismedi.constructionproject.api.feign.RemoteConstructionProjectFeignApi;
import com.smedi.ismedi.constructionproject.api.feign.RemoteProjectAssignmentFeignApi;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import smedi.ismedi.web.bff.controller.BaseController;

import javax.validation.Valid;
import java.util.List;

/**
 * 项目管理接口
 *
 * @author xulinjun
 */
@Slf4j
@RestController
@Api(tags = {"项目管理接口"})
@RequestMapping("/webbff/v1/constructionProject")
@AllArgsConstructor
public class ConstructionProjectController extends BaseController {
    private final RemoteConstructionProjectFeignApi remoteConstructionProjectFeignApi;
    private final RemoteProjectAssignmentFeignApi remoteProjectAssignmentFeignApi;

    /**
     * 保存项目信息
     *
     * @param
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "保存项目信息")
    @PostMapping(value = "/saveProject")
    public ResponseData<ProjectBasicResponse> postSaveProjectInfo(@Validated @RequestBody ProjectBasicRequest projectBasicRequest) {
        return remoteConstructionProjectFeignApi.postSaveProjectInfo(projectBasicRequest);
    }

    /**
     * 更新 项目 信息
     *
     * @param projectBasicUpdateRequest 项目更新信息
     * @return ResponseData<ProjectBasicResponse>
     */
    @ApiOperation(httpMethod = "POST", value = "更新 项目 信息")
    @PostMapping(value = "/updateProjectInfo")
    ResponseData<Boolean> updateProjectInfo(@Valid @RequestBody ProjectBasicUpdateRequest projectBasicUpdateRequest){
        return remoteConstructionProjectFeignApi.updateProjectInfo(projectBasicUpdateRequest);
    }


    /**
     * 项目组织界面查询项目分页信息
     *
     * @param offset
     * @param pageSize
     * @param projectName
     * @param projectCode
     * @param projectManager
     * @param hrOrganizationId
     * @param containHrOrganizationChildren
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "项目组织界面查询项目分页信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "offset", value = "页码", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "projectName", value = "项目名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "projectCode", value = "项目编码", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "projectManager", value = "项目经理", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "hrOrganizationId", value = "企业组织id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "containHrOrganizationChildren", value = "包含下级", dataType = "Boolean", allowableValues = "true, false")
    })
    @GetMapping(value = "/queryProjectPagination")
    public ResponseData<PaginationData> getQueryProjectPagination(@RequestParam("offset") Long offset,
                                                                  @RequestParam("pageSize") Long pageSize,
                                                                  @RequestParam(value = "projectName", required = false) String projectName,
                                                                  @RequestParam(value = "projectCode", required = false) String projectCode,
                                                                  @RequestParam(value = "projectManager", required = false) String projectManager,
                                                                  @RequestParam(value = "hrOrganizationId") Long hrOrganizationId,
                                                                  @RequestParam(value = "containHrOrganizationChildren", required = false) boolean containHrOrganizationChildren) {
        return remoteConstructionProjectFeignApi.getQueryProjectPagination(offset, pageSize, projectName, projectCode, projectManager, hrOrganizationId, containHrOrganizationChildren);
    }

    /**
     * 查询项目信息
     *
     * @param projectId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "查询项目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping(value = "/getQueryProjectBasicView")
    public ResponseData<ProjectBasicResponse> getQueryProjectBasicView(@RequestParam("projectId") Long projectId) {
        return remoteConstructionProjectFeignApi.getQueryProjectBasicView(projectId);
    }

    /**
     * 判断项目是否可以用
     *
     * @param projectId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "判断项目是否可以用")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping(value = "/checkProjectAvailable")
    public ResponseData<Boolean> getCheckProjectAvailable(@RequestParam("projectId") Long projectId) {
        return remoteConstructionProjectFeignApi.getCheckProjectAvailable(projectId);
    }

    /**
     * 判断组织下是否含有项目信息
     *
     * @param hrOrganizationId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "判断组织下是否含有项目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "hrOrganizationId", value = "企业组织id", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping(value = "/checkHrOrganizationHasProject")
    public ResponseData<Boolean> getCheckHrOrganizationHasProject(@RequestParam("hrOrganizationId") Long hrOrganizationId) {
        return remoteConstructionProjectFeignApi.getCheckProjectAvailable(hrOrganizationId);
    }

    /**
     * 项目管理界面查询项目分页信息
     *
     * @param offset
     * @param pageSize
     * @param projectName
     * @param projectCode
     * @param projectManager
     * @param hrOrganizationId
     * @param projectStatus
     * @param projectCategory
     * @param contractDurationBegin
     * @param contractDurationEnd
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "项目管理界面查询项目分页信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "offset", value = "页码", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "projectName", value = "项目名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "projectCode", value = "项目编码", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "projectManager", value = "项目经理", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "hrOrganizationId", value = "企业组织id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "supervisionUnit", value = "施工监理单位", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "projectStatus", value = "工程状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "projectCategory", value = "工程类别", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "contractDurationBegin", value = "合同工期开始：YYYY-MM-DD", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "contractDurationEnd", value = "合同工期结束：YYYY-MM-DD", dataType = "String", dataTypeClass = String.class),
    })
    @GetMapping(value = "/queryProjectManagementInfoPagination")
    ResponseData<PaginationData> getQueryProjectManagementInfoPagination(@RequestParam("offset") Long offset,
                                                                         @RequestParam("pageSize") Long pageSize,
                                                                         @RequestParam(value = "projectName", required = false) String projectName,
                                                                         @RequestParam(value = "projectCode", required = false) String projectCode,
                                                                         @RequestParam(value = "projectManager", required = false) String projectManager,
                                                                         @RequestParam(value = "hrOrganizationId", required = false) Long hrOrganizationId,
                                                                         @RequestParam(value = "supervisionUnit", required = false) String supervisionUnit,
                                                                         @RequestParam(value = "projectStatus", required = false) String projectStatus,
                                                                         @RequestParam(value = "projectCategory", required = false) String projectCategory,
                                                                         @RequestParam(value = "contractDurationBegin", required = false) String contractDurationBegin,
                                                                         @RequestParam(value = "contractDurationEnd", required = false) String contractDurationEnd) {
        return remoteConstructionProjectFeignApi.getQueryProjectManagementInfoPagination(offset, pageSize, projectName, projectCode, projectManager, hrOrganizationId, supervisionUnit, projectStatus, projectCategory, contractDurationBegin, contractDurationEnd);
    }

    /**
     * 项目组织获取可见的项目列表
     *
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "项目组织获取可见的项目列表")
    @GetMapping(value = "/queryAvailableProjectBasicList")
    public ResponseData<List<ProjectBasicResponse>> getQueryAvailableProjectBasicList() {
        return remoteConstructionProjectFeignApi.getQueryAvailableProjectBasicList();
    }

    /**
     * 项目组织获取可见的项目列表(带token)
     *
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "项目组织获取可见的项目列表(带token)")
    @GetMapping(value = "/queryAvailableProjectBasicListWithToken")
    public ResponseData<List<ProjectBasicResponse>> getQueryAvailableProjectBasicListWithToken() {
        return remoteConstructionProjectFeignApi.getQueryAvailableProjectBasicListWithToken();
    }

    /**
     * 获取企业组织下的项目数据列表
     *
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "获取企业组织下的项目数据列表")
    @GetMapping(value = "/getProjectTreeWithHrOrganizations")
    public ResponseData<ProjectTreeResponse> getProjectTreeWithHrOrganizations() {
        return remoteConstructionProjectFeignApi.getProjectTreeWithHrOrganizations();
    }

    /**
     * 删除项目、项目分配、项目标段信息
     *
     * @param projectId
     * @return
     */
    @ApiOperation(httpMethod = "DELETE", value = "删除项目、项目分配、项目标段信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
    })
    @DeleteMapping(value = "/deleteProjectAllInfos")
    public ResponseData<Boolean> deleteProjectAllInfos(@RequestParam("projectId") Long projectId) {
        return remoteConstructionProjectFeignApi.deleteProjectAllInfos(projectId);
    }

    /**
     * 根据传入的企业组织id获取项目列表
     *
     * @param hrOrganizationId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据传入的企业组织id获取项目列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "hrOrganizationId", value = "企业组织id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/getQueryProjectBasicListByHrOrganizationId")
    public ResponseData<List<ProjectBasicResponse>> getQueryProjectBasicListByHrOrganizationId(@RequestParam("hrOrganizationId") Long hrOrganizationId) {
        return remoteConstructionProjectFeignApi.getQueryProjectBasicListByHrOrganizationId(hrOrganizationId);
    }

    /**
     * 设置监控类型
     *
     * @param
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "设置监控类型")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目Id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query", name = "monitorType", value = "监控类型", dataType = "String", dataTypeClass = String.class),
    })
    @PostMapping(value = "/saveMonitorType")
    ResponseData<ProjectBasicResponse> postSaveMonitorType(@RequestParam("projectId") Long projectId,
                                                           @RequestParam("monitorType") String monitorType,
                                                           @RequestParam(value = "monitorTypeNote", required = false) String monitorTypeNote) {
        return remoteConstructionProjectFeignApi.postSaveMonitorType(projectId, monitorType, monitorTypeNote);
    }

    /**
     * 查询 项目 列表
     *
     * @param projectListQueryRequest 项目列表查询条件
     * @return ResponseData<List<ProjectResp>>
     */
    @ApiOperation(httpMethod = "POST", value = "查询 项目 列表")
    @PostMapping(value = "/getProjectList")
    ResponseData<List<ProjectResp>> getProjectList(@Valid @RequestBody ProjectListQueryRequest projectListQueryRequest){
        return remoteConstructionProjectFeignApi.getProjectList(projectListQueryRequest);
    }


    /**
     * 查询 项目（详情） 列表
     *
     * @param projectListQueryRequest 项目列表查询条件
     * @return ResponseData<List<ProjectResp>>
     */
    @ApiOperation(httpMethod = "POST", value = "查询 项目（详情） 列表")
    @PostMapping(value = "/getProjectDetailList")
    ResponseData<List<ProjectDetailResp>> getProjectDetailList(@Valid @RequestBody ProjectListQueryRequest projectListQueryRequest) {
        return remoteConstructionProjectFeignApi.getProjectDetailList(projectListQueryRequest);
    }

    /**
     * 查询 项目（详细） 分页
     *
     * @param projectPageQueryRequest 项目 分页 查询条件
     * @return ResponseData<List<ProjectResp>>
     */
    @ApiOperation(httpMethod = "POST", value = "查询 项目（详细） 分页")
    @PostMapping(value = "/getProjectDetailPage")
    ResponseData<PaginationData<ProjectDetailResp>> getProjectDetailPage(@Valid @RequestBody ProjectPageQueryRequest projectPageQueryRequest){
        return remoteConstructionProjectFeignApi.getProjectDetailPage(projectPageQueryRequest);
    }

    /**
     * 根据人员id获取项目分配信息
     *
     * @param personId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据人员id获取项目分配信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "personId", value = "人员id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/queryProjectAssignmentsByPersonId")
    public ResponseData<List<ProjectAssignmentResponse>> getQueryProjectAssignmentsByPersonId(@RequestParam("personId") Long personId) {
        return remoteProjectAssignmentFeignApi.getQueryProjectAssignmentsByPersonId(personId);
    }
}
